/*
 *  Kara IM - Kara Instant Messenger
 *  Copyright (C) 2007-2008 Markus Karnik
 *
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 3 of the License, 
 *  or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along 
 *  with this program; if not, see <http://www.gnu.org/licenses/>.
 *  
 */

package de.karnik.karaim;

import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.widget.EditText;

/**
 * This class implements the chat input dialog.
 * 
 * @author Markus Karnik - markus.karnik@my-design.org
 * @version 1.0
 * @since 1.0
 */
public class KaraInputDialog extends Dialog {

	/**
	 * The input dialog listener.
	 */
	private InputDialogListener inputDialogListener;
	/**
	 * The input text field.
	 */
	private EditText inputText;
	
	/**
	 * Creates the input dialog from the given context an title.
	 * 
	 * @param context The invoking context object.
	 * @param title The title to display.
	 */
	public KaraInputDialog( Context context, String title ) {
		super( context );

		this.setContentView( R.layout.input_dialog );
		this.setTitle( title );
		
		inputText = ( EditText )this.findViewById( R.id.text_send );
	}

    /* (non-Javadoc)
     * @see android.app.Dialog#dispatchKeyEvent(android.view.KeyEvent)
     */
    public boolean dispatchKeyEvent( KeyEvent keyEvent ) {
    	
    	int keyCode = keyEvent.getKeyCode();
    	
    	if( ( keyCode == KeyEvent.KEYCODE_DPAD_CENTER ) ) {
        	
    		String text =  inputText.getText().toString();
    		
    		if( inputDialogListener != null && text.length() > 0 )
    			inputDialogListener.messageEntered( text );
    		
    		inputText.setText( "" );
    		
    	}    	
    	
    	return super.dispatchKeyEvent( keyEvent );
    }
	
	/**
	 * Removes the input dialog listener.
	 */
	public void removeInputDialogListener() {
		inputDialogListener = null;
	}

	/**
	 * Sets the input dialog listener.
	 * 
	 * @param inputDialogListener The input dialog listener to set.
	 */
	public void setInputDialogListener(InputDialogListener inputDialogListener) {
		this.inputDialogListener = inputDialogListener;
	}
}